package com.example.recycleviewdemo;

import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity implements  View.OnClickListener {

    private RecyclerView recyclerView;
    private MyItemDecoration myItemDecoration;
    private static Random mRandom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mRandom = new Random();
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        findViewById(R.id.btn_type_list).setOnClickListener(this);
        findViewById(R.id.btn_type_grid).setOnClickListener(this);
        findViewById(R.id.btn_type_pbl).setOnClickListener(this);

        initData();
    }

    private void initData() {
        List<String> mDatas = new ArrayList<>();
        for (int i = 0; i < 100; i ++ ){
            mDatas.add("赵冲" + i);
        }
        final MyAdapter mAdapter = new MyAdapter();
        mAdapter.add(mDatas);

        mAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(view.getContext(),((TextView)view).getText(),Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(View view, int position) {
                mAdapter.remove(position);
            }
        });
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(mAdapter);
    }

    static class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{

        private OnItemClickListener onItemClickListener;

        public void  setOnItemClickListener(OnItemClickListener l){
            this.onItemClickListener = l;
        }

        public interface OnItemClickListener{
            public void onItemClick(View view,int position);
            public void onItemLongClick(View view, int position);
        }

        private  List<String> mList = new ArrayList<>();


        public void add(List<String> value){
            mList.addAll(value);

            notifyDataSetChanged();
        }

        public void remove(int position){
            mList.remove(position);
            notifyItemRemoved(position);
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            View view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_expandable_list_item_1, parent, false);

            CardView cardView = new CardView(parent.getContext());
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.height = mRandom.nextInt(100)+80;
            cardView.setLayoutParams(layoutParams);

            cardView.setZ(3f);

            cardView.addView(view);

            return new MyViewHolder(cardView);
        }

        @Override
        public void onBindViewHolder(final MyViewHolder holder, int position) {
            holder.textView.setText(mList.get(position));

            if (onItemClickListener != null){
                holder.cardView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getLayoutPosition();
                        onItemClickListener.onItemClick(holder.textView,pos);
                    }
                });

                holder.cardView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int pos = holder.getLayoutPosition();
                        onItemClickListener.onItemLongClick(holder.textView,pos);
                        return true;
                    }
                });
            }
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }
    }

    static class  MyViewHolder extends  RecyclerView.ViewHolder{
        public CardView cardView;
        public TextView textView;
        public MyViewHolder(View itemView) {
            super(itemView);

            cardView = (CardView) itemView;
            textView = (TextView) cardView.findViewById(android.R.id.text1);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_type_list:
                setType(0);

                break;
            case R.id.btn_type_grid:
                setType(1);
                break;
            case R.id.btn_type_pbl:
                setType(2);
                break;
        }
    }

    private void setType(int type) {
        RecyclerView.LayoutManager manager = null;
        if (myItemDecoration == null){
            myItemDecoration = new MyItemDecoration(5);
        }
        recyclerView.removeItemDecoration(myItemDecoration);
        switch (type){
            case 0:  //list
                manager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
                break;
            case 1:  //grid
                manager = new GridLayoutManager(this,3);
                break;
            case 2:  //list

                StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);


                recyclerView.addItemDecoration(myItemDecoration);
                manager = staggeredGridLayoutManager;

                break;

            default:
                manager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
                break;
        }
        recyclerView.setLayoutManager(manager);

    }
    class MyItemDecoration extends RecyclerView.ItemDecoration{
        private  int mValue;
        public MyItemDecoration(int value){
            this.mValue = value;
        }


        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.bottom = mValue;
            outRect.left = mValue;
            outRect.right = mValue;
            if (parent.getChildAdapterPosition(view) % 3 == 0){
                outRect.top = mValue;
            }
        }
    }
}
